package cn.iocoder.yudao.module.promotion.seckill.dal.mysql;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.framework.business.basic.dao.query.CoreLambdaQueryWrapper;
import cn.iocoder.yudao.framework.business.basic.dao.query.CoreQueryWrapper;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.promotion.activity.dal.mysql.ActivityMapper;
import cn.iocoder.yudao.module.promotion.seckill.dal.dataobject.SeckillActivityDO;
import cn.iocoder.yudao.module.promotion.seckill.vo.AppSeckillActivityPageReqVO;
import cn.iocoder.yudao.module.promotion.seckill.vo.SeckillActivityQueryReqDTO;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.apache.ibatis.annotations.Mapper;

import java.time.LocalDateTime;

/**
 * 秒杀活动 Mapper
 *
 * @author halfninety
 */
@SuppressWarnings("UnusedReturnValue")
@Mapper
public interface SeckillActivityMapper extends ActivityMapper<SeckillActivityDO> {

    default PageResult<SeckillActivityDO> selectPage(Integer current, Integer size, SeckillActivityQueryReqDTO reqVO) {
        CoreQueryWrapper<SeckillActivityDO> wrapper = new CoreQueryWrapper<>();
        wrapper.likeIfPresent("name", reqVO.getName());
        wrapper.eqIfPresent("status", reqVO.getStatus());
        wrapper.apply(reqVO.getConfigId() != null && reqVO.getConfigId() > 0, "FIND_IN_SET({0}, config_ids) > 0", reqVO.getConfigId());
        wrapper.orderByAsc("sort");
        wrapper.orderByDesc("start_time");
        return selectPage(current, size, wrapper);
    }

    /**
     * 更新活动库存(减少)
     *
     * @param id    活动编号
     * @param count 扣减的库存数量(正数)
     * @return 影响的行数
     */
    default int updateStockDecr(Long id, int count) {
        Assert.isTrue(count > 0);
        return update(null, new LambdaUpdateWrapper<SeckillActivityDO>()
                .eq(SeckillActivityDO::getId, id)
                .ge(SeckillActivityDO::getStock, count)
                .setSql("stock = stock - " + count));
    }

    /**
     * 更新活动库存（增加）
     *
     * @param id    活动编号
     * @param count 增加的库存数量(正数)
     * @return 影响的行数
     */
    default int updateStockIncr(Long id, int count) {
        Assert.isTrue(count > 0);
        return update(null, new LambdaUpdateWrapper<SeckillActivityDO>()
                .eq(SeckillActivityDO::getId, id)
                .setSql("stock = stock + " + count));
    }

    default PageResult<SeckillActivityDO> selectPage(AppSeckillActivityPageReqVO pageReqVO, Integer status, LocalDateTime dateTime) {
        return selectPage(pageReqVO, new CoreLambdaQueryWrapper<SeckillActivityDO>()
                .eqIfPresent(SeckillActivityDO::getStatus, status)
                .lt(SeckillActivityDO::getStartTime, dateTime)
                .gt(SeckillActivityDO::getEndTime, dateTime)// 开始时间 < 指定时间 < 结束时间，也就是说获取指定时间段的活动
                .apply(ObjectUtil.isNotNull(pageReqVO.getConfigId()), "FIND_IN_SET({0},config_ids) > 0", pageReqVO.getConfigId())
                .orderByAsc(SeckillActivityDO::getSort)
                .orderByDesc(SeckillActivityDO::getStartTime));
    }

}
